﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using PepsiCo.Common;
using System.Text;
using System.Collections.Generic;
using T2V.SharePoint.Common.Utilities.Camlex;
using System.Linq.Expressions;
using Microsoft.SharePoint.Utilities;

namespace PepsiCo.Intranet.WebParts.MegaMenu
{
    public partial class MegaMenuUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadMenu();
        }

        private void LoadMenu()
        {
            SPWeb rootWeb = SPContext.Current.Site.RootWeb;

            SPList splistMenuSections = rootWeb.GetList(rootWeb.ServerRelativeUrl.TrimEnd('/') + "/" + PepsiCoConstants.MegaMenuSection.LIST_URL.TrimStart('/'));
            SPList splistMenuItems = rootWeb.GetList(rootWeb.ServerRelativeUrl.TrimEnd('/') + "/" + PepsiCoConstants.MegaMenuItem.LIST_URL.TrimStart('/'));

            if (splistMenuSections != null && splistMenuItems != null)
            {
                StringBuilder htmlBuilder = new StringBuilder();

                htmlBuilder.Append("<div class='blue'><ul id='mega-menu' class='mega-menu'>");

                string camlSec = string.Empty;
                var secExpressionsAnd = new List<Expression<Func<SPListItem, bool>>>();
                secExpressionsAnd.Add(x => (bool)x[PepsiCoConstants.MegaMenuSection.IS_ACTIVE_COLUMN] == true);
                camlSec = Camlex.Query().WhereAll(secExpressionsAnd).OrderBy(x => x[PepsiCoConstants.MegaMenuSection.ITEM_ORDER_COLUMN] as Camlex.Asc).ToString();

                SPQuery secQry = new SPQuery();
                secQry.Query = camlSec;
                SPListItemCollection secItems = splistMenuSections.GetItems(secQry);

                //Render Items                    
                foreach (SPListItem section in secItems)
                {
                    htmlBuilder.Append("<li><a href='" + section[PepsiCoConstants.MegaMenuSection.HYPERLINK_COLUMN].ToString() + "'>" + SPEncode.HtmlEncode(section[PepsiCoConstants.MegaMenuSection.SECTION_COLUMN].ToString()) + "</a>");

                    string camlItem = string.Empty;
                    var itemExpressionsAnd = new List<Expression<Func<SPListItem, bool>>>();
                    itemExpressionsAnd.Add(x => x[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN] == (DataTypes.LookupId)section.ID.ToString());
                    itemExpressionsAnd.Add(x => (bool)x[PepsiCoConstants.MegaMenuItem.IS_ACTIVE_COLUMN] == true);
                    camlItem = Camlex.Query().WhereAll(itemExpressionsAnd).OrderBy(x => new[] { x[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN] as Camlex.Asc, x[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] as Camlex.Asc }).ToString();

                    SPQuery itemQry = new SPQuery();
                    itemQry.Query = camlItem;
                    SPListItemCollection linkItems = splistMenuItems.GetItems(itemQry);

                    String lastSec = null;
                    if (linkItems != null && linkItems.Count > 0)
                    {
                        htmlBuilder.Append("<ul>");
                    }
                    foreach (SPListItem link in linkItems)
                    {
                        string subSectionText = string.Empty;
                        if (link[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN] != null && !string.IsNullOrEmpty(link[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim()))
                        {
                            subSectionText = link[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim();
                        }

                        if (lastSec != subSectionText)
                        {
                            if (lastSec != null)
                            {
                                htmlBuilder.Append("</ul></li>");
                            }
                            if (!string.IsNullOrEmpty(subSectionText))
                            {
                                var subSecArr = subSectionText.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

                                if (subSectionText.IndexOf(";#") != -1 && subSecArr.Length == 1)
                                {
                                    htmlBuilder.Append("<li><ul>");
                                }
                                else
                                {
                                    htmlBuilder.Append("<li><div class='subhead'>" + SPEncode.HtmlEncode(subSecArr.Length > 1 ? subSecArr[1] : subSecArr[0]) + "</div><ul>");
                                }    
                            }
                            else
                            {
                                htmlBuilder.Append("<li><ul>");
                            }
                        }

                        htmlBuilder.Append("<li><a href='" + link[PepsiCoConstants.MegaMenuItem.HYPERLINK_COLUMN].ToString() + "'>" + SPEncode.HtmlEncode(link[PepsiCoConstants.MegaMenuItem.TITLE_COLUMN].ToString().Trim()) + "</a></li>");

                        lastSec = subSectionText;

                    }
                    if (linkItems != null && linkItems.Count > 0)
                    {
                        htmlBuilder.Append("</ul></li></ul>");
                    }

                    htmlBuilder.Append("</li>");

                }
                htmlBuilder.Append("</ul></div>");

                ltMegaMenu.Text = htmlBuilder.ToString();
            }

            //SPList headerMenuList = rootWeb.GetList(rootWeb.ServerRelativeUrl.TrimEnd('/') + "/" + Constants.HEADER_MENU_LIST_URL.TrimStart('/'));

            //if (headerMenuList != null)
            //{
            //    StringBuilder htmlBuilder = new StringBuilder();

            //    string caml = string.Empty;
            //    var expressionsAnd = new List<Expression<Func<SPListItem, bool>>>();

            //    expressionsAnd.Add(x => ((bool)x[Constants.ACTIVE_COLUMN]) == true);

            //    caml = Camlex.Query().WhereAll(expressionsAnd).OrderBy(x => x[Constants.ORDER_NUMBER_COLUMN] as Camlex.Asc).ToString();

            //    SPQuery spQuery = new SPQuery();
            //    spQuery.Query = caml;

            //    SPListItemCollection items = headerMenuList.GetItems(spQuery);

            //    if (items != null && items.Count > 0)
            //    {
            //        htmlBuilder.Append("<ul>");

            //        foreach (SPListItem item in items)
            //        {
            //            if (item["URL"] != null)
            //            {
            //                SPFieldUrlValue urlValue = new SPFieldUrlValue(item["URL"].ToString());

            //                htmlBuilder.AppendFormat("<li><a href='{0}'>{1}</a></li>", urlValue.Url, item.Title);
            //            }
            //            else
            //            {
            //                htmlBuilder.AppendFormat("<li><a href='#'>{0}</a></li>", item.Title);
            //            }
            //        }
            //        htmlBuilder.Append("</ul>");

            //        ltHeaderMenu.Text = htmlBuilder.ToString();
            //    }
            //}
        }
    }
}
